<?php
/*
* $Id: user_mod.php 93 2010-07-27 09:36:19Z heriniaina.eugene $
*/

class user extends module {

	public $title = "User Module";
	public $author = "Hery";
	public $version = "0.1";
	
	private $endpoint = "http://login.serasera.org/wsdl/?wsdl";
	private $wsid = "karajia";
	public $info = array();
	
	public function init()
	{
		$this->timerClass->addTimer("updateini", $this, "updateIni", "", 60*15, false);
		$this->user_ini = new ini("data/users.ini");
	}

	public function destroy()
	{
		$this->timerClass->removeTimer("updateini");
	}

	public function printError($line)
	{
		$this->ircClass->privMsg($line['fromNick'], "Tsy afaka mampiasa an'io baiko io ianao raha tsy mampiasa ny baiko " . BOLD . "!login solonanarana tenimiafina");
		$this->ircClass->privMsg($line['fromNick'], "Raha tsy manana " . BOLD . "solonanarana" . BOLD . " sy " . BOLD . "tenimiafina" . BOLD . " dia misorata anarana ao amin'ny " . BLUE . "http://login.serasera.org");
	
	}
	// Write to file
	public function updateIni($args)
	{
		if ($this->user_ini->getError())
		{
			return;
		}

		$this->user_ini->writeIni();

		return true;
	}
	
	
	public function loggedin($line)
	{
		
		if (
		($this->user_ini->getIniVal($line['fromNick'], 'time') > time()) &&
		($line['fromHost'] == $this->user_ini->getIniVal($line['fromNick'], 'host')))
		{
			$this->user_ini->setIniVal($line['fromNick'], 'time', time() + (60*15));
			
			
			return $this->user_ini->getVars($line['fromNick']);
		}
		else
		{
		
			return false;
		}
		 
	}
	
	public function logout($line, $args)
	{
		$this->user_ini->setIniVal($line['fromNick'], 'time', 0);
		$this->ircClass->privMsg($line['fromNick'], "Tafavoaka soa amantsara ianao.");
	}
	
	
	public function update($line, $args)
	{
		if ($this->loggedin($line))
		{
			$this->user_ini->setIniVal($line['fromNick'], 'time', time() + (60*15));
		}
	}
	
	// Update actions
	public function login($line, $args)
	{
		if(isset($args['arg1']) && isset($args['arg2'])) {
			$username = $args['arg1'];
			$password = $args['arg2'];
			if($this->auth( $username, $password )) {
				$this->user_ini->setIniVal($line['fromNick'], 'username', $username);
				$this->user_ini->setIniVal($line['fromNick'], 'nick', $line['fromNick']);
				$this->user_ini->setIniVal($line['fromNick'], 'host', $line['fromHost']);
				$this->user_ini->setIniVal($line['fromNick'], 'time', time() + (60*15));
				
				
				$this->ircClass->privMsg($line['fromNick'], "Tafiditra soa amantsara ianao. Raha mitovy amin'ny solonanarana entinao ao amin'ny serasera ny solonanarana entinao eto dia miakatra ho " . BOLD . "VOICE" . BOLD . " ianao.");
				$this->ircClass->privMsg($line['fromNick'], RED . "Soraty hoe " . BLUE . "!vonjy" . RED . " raha hijery ny baiko azonao ampiasaina.");
				
				if($line['fromNick'] == $username) {
					$this->ircClass->changeMode("#serasera", "+", "v", $line['fromNick']);
				}
				
			} else {
				$this->ircClass->privMsg($line['fromNick'], "Diso ny solonanarana na ny tenimiafina. Avereno indray.");
			}
		} else {
			$this->ircClass->privMsg($line['fromNick'], "Mba hahafahanao miditra mampiasa ireo tolotra isan-karazany dia soraty " . BOLD . "!login solonanarana tenimiafina");
		}
		Return ;
	}
	
	function auth( $user, $pass )
	{
		require_once("libraries/nusoap/nusoap_mod.php");
		$client = new nusoapclient($this->endpoint, true);

		$err = $client->getError();
		
		if($err) {

			Return false;
		}

		$param = array('wsid' => $this->wsid, 'username' => $user, 'password' => $pass, 'site' => 'Chat karajia');
		$result = $client->call('getUser', $param);
		

		if ($client->fault) {
			Return false;
		} else {
			Return ($result['loggedin'] == true);
		}
	}
	

}

?>
